﻿using System;
using System.Collections.Specialized;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.Reflection;
using System.Xml.Serialization;

namespace MoneyTrackin
{
    [XmlType]
    public class ConfigurationSettings
    {
        public static string MD5(string password) 
        {
		    byte[] textBytes = System.Text.Encoding.Default.GetBytes(password);
		    try {
			    System.Security.Cryptography.MD5CryptoServiceProvider cryptHandler;
			    cryptHandler = new System.Security.Cryptography.MD5CryptoServiceProvider();
			    byte[] hash = cryptHandler.ComputeHash (textBytes);
			    string ret = "";
			    foreach (byte a in hash) {
				    if (a<16)
					    ret += "0" + a.ToString ("x");
				    else
					    ret += a.ToString ("x");
			    }
			    return ret ;
		    }
		    catch {
			    throw;
		    }
	    }

        
        private string _login;
        [XmlElement]
        public string Login
        {
            get
            {
                return _login;
            }
            set
            {
                _login = value;
            }
        }
        
        private string _password;
        [XmlElement]
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                _password = value;
            }
        }
        
        private string _projectID;
        [XmlElement]
        public string ProjectID
        {
            get
            {
                return _projectID;
            }
            set
            {
                _projectID = value;
            }
        }

        private static string configPath;
        static ConfigurationSettings()
        {
            configPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            configPath = configPath.Replace(@"file:\", string.Empty);
            configPath = Path.Combine(configPath, "application.config");
        }
        public void Save()
        {
            XmlSerializer serializer2 = new
                XmlSerializer(Instance.GetType());
            TextWriter writer = new StreamWriter(configPath);
            serializer2.Serialize(writer, this);
            writer.Close();
        }
        private void Load()
        {
            if (File.Exists(configPath))
            {
                XmlSerializer serializer = new
                XmlSerializer(typeof(ConfigurationSettings));
                TextReader reader = new StreamReader(configPath);
                try
                {
                    Instance = serializer.Deserialize(reader) as ConfigurationSettings;
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message, "app.config error");
                }
                finally
                {
                    reader.Close();
                }
            }
            else
            {
                Save();
            }
        }

        private static ConfigurationSettings _instance;
        public static ConfigurationSettings Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ConfigurationSettings();
                    _instance.Load();
                }
                return _instance;
            }
            private set
            {
                _instance = value;
            }
        }
    }
}